package com.yz.dba.thread;


import com.yz.common.utils.JdbcUtils;
import com.yz.dba.service.TaskCountService;
import com.yz.dba.utils.SpringContextUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author 段成
 * @date 2019/12/3 - 20:35
 */
public class ThreadCoreTest implements Runnable{

    /**
     * 用SpringContextUtil间接从spring容器中注入
     */
    TaskCountService taskCountService = SpringContextUtil.getBean("taskCountService");

    StringRedisTemplate redisTemplate = SpringContextUtil.getBean("stringRedisTemplate");


    private Map dataSourceMap;
    private Map targetMap;

    private String sourceTableName;
    private String targetTableName;
    private String taskNumber;
    private String fieldNames;
    private String corefields;


    public ThreadCoreTest(Map dataSourceMap, Map targetMap, String sourceTableName, String targetTableName, String taskNumber, String corefields) {
        this.dataSourceMap = dataSourceMap;
        this.targetMap = targetMap;
        this.sourceTableName = sourceTableName;
        this.targetTableName = targetTableName;
        this.taskNumber = taskNumber;
        this.corefields = corefields;
    }

    @Override
    public void run() {
        //初始化task_count redis
        redisTemplate.opsForValue().set("ThreadCount_"+taskNumber,"0");
        //初始化task_count mysql
        taskCountService.initializeTaskCount(taskNumber);



        //获取表总记录数
        int total = JdbcUtils.queryCount(dataSourceMap, sourceTableName);

        //获取分页数据 当前页 分页单位 最后一页记录数


        int pageSize = 1000;

        if(total<pageSize){
            StringBuffer querySql = new StringBuffer();
            querySql.append("select * from ");
            querySql.append(sourceTableName);
            System.out.println("查询语句:"+querySql.toString());
            //开启子线程
            ThreadCoreUtil threadUtil = new ThreadCoreUtil(dataSourceMap,targetMap, sourceTableName,targetTableName, querySql.toString(),taskNumber,corefields);

            new Thread(threadUtil).start();
        }else {
            int lastPageCount = total%pageSize;
            int totalPages = total/pageSize==0?(total/pageSize):(total/pageSize+1);




            //生成查询sql
            ArrayList<String> sqlList = new ArrayList<>();
            int count = 1;
            for(int pageNum=0;pageNum<totalPages;pageNum++){
                //totalPages 101
                count++;
                StringBuffer querySql = new StringBuffer();
                querySql.append("select * from ");
                querySql.append(sourceTableName);
                querySql.append(" limit ");
                //pageIndex 为 (pageNum-1)*pageSize 此时的pageNum是从0开始的 不再-1
                querySql.append(pageNum*pageSize);
                querySql.append(",");
                querySql.append(pageSize);
                System.out.println("查询语句:"+querySql.toString());
                //sqlList.add(querySql.toString());
                if(count==10){
                    //进入递归
                    checkTaskCount(taskNumber);
                    count=0;
                }

                //开启子线程
                ThreadCoreUtil threadUtil = new ThreadCoreUtil(dataSourceMap,targetMap, sourceTableName,targetTableName, querySql.toString(),taskNumber,corefields);

                new Thread(threadUtil).start();
            }
        }





    }

    public void checkTaskCount(String taskNumber){
        try {
            //睡眠1500毫秒  等待执行线程结束
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //查redis中记录的线程数
        StringRedisTemplate redisTemplate = SpringContextUtil.getBean("stringRedisTemplate");
        int threadCount = Integer.parseInt(redisTemplate.opsForValue().get("ThreadCount_"+taskNumber));

        //查mysql中记录的线程数
        int taskCount = taskCountService.queryTaskCount(taskNumber);
        //若==0 则线程执行结束
        if(taskCount == 0){
            return;
        }
        //否则继续递归查询
        checkTaskCount(taskNumber);
    }


}
